﻿using System;
using Framework.MVC;
using Log.Model;
using Billing.Model;
using Billing.IService.Entry;
using Recharge.Entry;
using Framework.MVVM.Model;
using Log.IService;
using Log.Service;
using Common.Service;
using Common.IService;
using Billing.IService;
using Billing.Service;
using Config.Model;
using Framework.Extention.Billing;
using Framework.Common.Entry;
using System.Collections.Generic;
using Config.IService;
using Config.Service;
using Recharge.Expand;


namespace Recharge.Models
{
    [ServiceAttribute(typeof(IConfigService), typeof(ConfigService), "Instance")]
    [ServiceAttribute(typeof(IRechargeOrderService), typeof(RechargeOrderService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class RechargeOrderDoneModel : AsyncModelBase, IWriteLog, IGetConfig<IDictionary<string, KeyValueEntry<string, string>>>, IGetConfig<IBilling>, IModifyRechargeOrderStatus, IGetRechargeOrder
    {
        #region Field

        private const string m_error_type = "donecge00";
        private const string m_complete_type = "donecge01";
        private const string m_error_validate = "donecge02";
        private const string m_complete_validate = "donecge03";
        private const string m_error_code = "donecge04";
        private const string m_error_status = "donecge05";
        private const string m_error_rechargeOrder = "donecge06";
        private const string m_complete_rechargeOrder = "donecge07";
        private const string m_done_rechargeOrder = "donecge08";

        private const string m_result = "result";

        private const string m_entry = "entry";

        private const string m_type_config = "data:recharge:type";

        #endregion Field

        #region Property

        public string Source { get; set; }

        public string Header { get; set; }

        #endregion Property

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            ConfigName = m_type_config;
            this.GetConfig<IDictionary<string, KeyValueEntry<string, string>>>(m_error_type, m_complete_type);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IGetConfig Billing Type

        public string Type { get; set; }

        public void OnGetConfigComplete(string key, IDictionary<string, KeyValueEntry<string, string>> entry)
        {
            if (entry.ContainsKey(Type))
            {
                OnBeginRequest();
                ConfigName = entry[Type].key;
                this.GetConfig<IBilling>(m_error_validate, m_complete_validate);
            }
            this.Log(key, Source);
        }

        #endregion IGetConfig Billing Type

        #region IGetConfig Billing

        public string ConfigName { get; set; }

        public KeyValueEntry<string, object>[] Params { get; set; }

        public void OnGetConfigComplete(string key, IBilling entry)
        {
            string code = string.Empty;
            if (entry.ValidateParams(out code, Params)) {
                OnBeginRequest();
                RechargeOrderCode = code;
                this.ModifyRechargeOrderStatus(m_error_code, m_error_status, m_error_rechargeOrder, m_complete_rechargeOrder);
            }
            this.Log(key, Source);
        }

        public void OnGetConfigError(string key)
        {
            this.Log(key, Source);
            
        }

        #endregion IGetConfig Billing

        #region IModifyRechargeOrderStatus

        public string RechargeOrderCode { get; set; }

        public int RechargeOrderStatus { get; set; }

        public void OnModifyRechargeOrderComplete(string key, RechargeOrderEntry entry)
        {
            OnBeginRequest();
            RechargeCenterOrderEntry _entry = new RechargeCenterOrderEntry {
                code = entry.code,
                amount = entry.amount,
                indate = entry.indate,
                mode = entry.sourceCode,
                owner = entry.owner,
                receiver = entry.receiver,
                status = entry.status,
                type = entry.type
            };
            OnBeginRequest();
            this.GetRechargeOrder("donecge09", "donecge10", "donecge11");

            OnComplete(m_result,true);
            this.Log(key, _entry);
            this.Log(m_done_rechargeOrder, Source);
        }

        public void OnModifyRechargeOrderError(string key)
        {
            this.Log(key, Source);
        }

        #endregion IModifyRechargeOrderStatus


        #region IGetRechargeOrder

        public void OnGetRechargeOrderComplete(string key, RechargeOrderEntry entry)
        {
            OnBeginRequest();
            string id = RechargeHelper.PostUser(Header, entry.receiver, entry.sourceCode);
            this.Log("hzw_id0", id);
            if (!string.IsNullOrWhiteSpace(id))
            {
                OnBeginRequest();
                this.Log("hzw_id1", id);
                string content = string.Empty;
                bool test = RechargeHelper.PostTemp(Header, id, entry.amount, entry.code, "S1", out content);
                OnBeginRequest();
                this.Log("hzw_id2", content);
            }
            OnComplete(m_entry, entry);
            this.Log(key, Source);
        }

        public void OnGetRechargeOrderError(string key)
        {
            this.Log(key, Source);
        }

        #endregion IGetRechargeOrder
        
    }
}